<?php
namespace app\adminjcb\controller;

use app\adminjcb\model\Ad as ArticleModel;
use app\common\controller\AdminBaseController;

/**
 * Class Article
 */
class AdController extends AdminBaseController {
    protected $article_model;

    protected function _initialize() {
        parent::_initialize();
        $this->article_model  = new ArticleModel();
    }

    /**
     * 文章管理
     * @param int    $cid 分类ID
     * @param string $keyword 关键词
     * @param int    $page
     * @return mixed
     */
    public function index() {
        $article_list  = $this->article_model->select();
        return $this->fetch('index', ['article_list' => $article_list]);
    }



	//添加幻灯片
	function add_upload(){
		if($this->request->isPost()){
			
			$data['thumb'] 		= input('thumb');
			$data['add_time'] 	= now_time();
			
			$result = db('ad')->insert($data);
			if($result){
				 $this->success('添加成功');
			}else{
				 $this->error('添加失败');
			}
			
		}
	}

    /**
     * 添加文章
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }

    /**
     * 保存文章
     */
    public function save() {
        if ($this->request->isPost()) {
        	$data['title'] = input('title');
			$data['url'] = input('url');
			$data['add_time'] = now_time();
			$data['thumb'] = input('thumb_img');
            if ($this->article_model->allowField(true)->save($data)) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 编辑文章
     * @param $id
     * @return mixed
     */
    public function edit($id) {
        $article = $this->article_model->find($id);
        return $this->fetch('edit', ['article' => $article]);
    }

    /**
     * 更新文章
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
        	$data['title'] = input('title');
			$data['url'] = input('url');
			if (input('thumb_img')) {
				$data['thumb'] = input('thumb_img');
			}
            if (db('ad')->where('id',$id)->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 删除文章
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = []) {
        $id = $ids ? $ids : $id;
        if ($id) {
            if ($this->article_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的');
        }
    }

}
